package com.sc.nft.controller;

import cn.hutool.core.util.StrUtil;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.*;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.ecology.*;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.EcologyReforgeConfService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/ecology/reforge")
@Api(tags = "生态重铸")
public class EcologyReforgeController {

    private final EcologyReforgeConfService ecologyReforgeConfService;
    private final Redisson redisson;

    @ApiOperation(value = "生态重铸列表", notes = "生态重铸列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<EcologyReforgeListVO> list(@Valid @RequestBody SaasIdPageReq request){
        UserInfo user = UserPool.getUser();
        return PageResult.success(ecologyReforgeConfService.list(request.getPageNo(),request.getPageSize(),request.getUserCreatorId(),user));
    }


    @ApiOperation(value = "生态重铸详情", notes = "生态重铸详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/details", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<EcologyReforgeDetailsVO> details(@Valid @RequestBody IdRequest request){
        return Result.success(ecologyReforgeConfService.details(request.getId(), UserPool.getUser()));
    }

    @ApiOperation(value = "获取重铸所需数量", notes = "获取重铸所需数量", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/preExpendDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<EcologyReforgeExpendVO>> preExpendDetails(@Valid @RequestBody PreExpendDetailsRequest request){
        return Result.success(ecologyReforgeConfService.preExpendDetails(request.getId(),request.getCount(), UserPool.getUser()));
    }

    @ApiOperation(value = "重铸", notes = "重铸", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/expend", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result expend(@Valid @RequestBody ExpendEcologyReforgeRequest request) throws GlobalException {
        UserInfo user = UserPool.getUser();
        if(!StrUtil.equals(request.getPayPwd(),user.getTransactionPassword())){
            return Result.fail(ErrorCode.PASSWORD_ERROR);
        }

        RLock lock = redisson.getLock(RedisKeyEnum.ECOLOGY_REFORGE_LOCK.getKey() + request.getId());
        try {
            boolean res = lock.tryLock(10, 10, TimeUnit.SECONDS);
            if (res) {
                ecologyReforgeConfService.expend(request.getId(),request.getCount(),user);
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new GlobalException("系统繁忙,请稍后重试!");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

    @ApiOperation(value = "重铸明细", notes = "重铸明细", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/expendRecords", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<ExpendEcologyReforgeRecordsVO> expendRecords(@Valid @RequestBody PageRequest request){
        return PageResult.success(ecologyReforgeConfService.expendRecords(UserPool.getUser(),request.getPageNo(),request.getPageSize()));
    }

    @ApiOperation(value = "重铸记录", notes = "铸造记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/expendRecordsPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<UserReforgeExpendRecordsPageVO> expendRecordsPage(@Valid @RequestBody SaasIdPageReq request){
        return PageResult.success(ecologyReforgeConfService.expendRecordsPage(UserPool.getUser(),request.getPageNo(),request.getPageSize(),request.getUserCreatorId()));
    }

    @ApiOperation(value = "重铸记录详情", notes = "铸造记录详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/expendRecordsDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<ExpendEcologyReforgeRecordsVO>> expendRecordsDetails(@Valid @RequestBody IdRequest request){
        return PageResult.success(ecologyReforgeConfService.expendRecordsDetails(UserPool.getUser(),request.getId()));
    }

    @ApiOperation(value = "重铸上限记录", notes = "重铸上限记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/userEcologyReforgeUpperLimitDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<UserEcologyReforgeUpperLimitDetailsVO> userEcologyCastUpperLimitDetails(@Valid @RequestBody IdPageRequest request){
        return PageResult.success(ecologyReforgeConfService.userEcologyReforgeUpperLimitDetails(UserPool.getUser(),request.getId(),request.getPageNo(),request.getPageSize()));
    }

}
